<?php

class Ext_Shopping_Show
{
    public $price;
    public $photo;
    public $siteName;
    public $siteViewUrl;

    public function __construct()
    {
        $this->photo = new Ext_Image_Show();
    }
    
    public function build($shoppingId)
    {
        $_shoppingRow = App_Model_DbTable_Shopping::getInstance()->getRowById($shoppingId);
        if(!$_shoppingRow){
            return "";
        }
        
        $this->price = $_shoppingRow['price'];
        
        $imageId = $_shoppingRow['imageId'];
        $host    = $_shoppingRow['host'];

        $this->photo->build($imageId);
        
        $config = Ext_Config::getInstance()->getConfig("attachment");
        $attachmentConfig = $config->toArray();
        if(!is_array($attachmentConfig)){
            $attachmentConfig = array();
        }
        if(!is_array($attachmentConfig['shopping'])){
            $attachmentConfig['shopping'] = array();
        }
        $hostAdapter = '';
        foreach($attachmentConfig['shopping'] as $hostKey=>$hostVal)
        {
            if(!isset($hostVal['host'])){
                continue;
            }
            $hostDomain = $hostVal['host'];
            if(preg_match("/".$hostDomain."$/si", $host)){
                $hostAdapter = $hostKey;
                break;
            }
        }
        if(!$hostAdapter)
        {
            require_once 'Ext/Shopping/Exception.php';
            throw new Ext_Shopping_Exception("Shopping show adapter '".$host."' not found a.");
        }
        
        $classname = "Ext_Shopping_Show_Adapter_".ucfirst($hostAdapter);
        if(!class_exists( $classname ))
        {
            require_once 'Ext/Shopping/Exception.php';
            throw new Ext_Shopping_Exception("Shopping show adapter '".$hostAdapter."' not found b.");
        }

        $_adapter = new $classname($shoppingId);
        
        $this->siteName     = $_adapter->siteName;
        $this->siteViewUrl  = $_adapter->siteViewUrl;
    }
}
